//
//  ResetPasswordViewController.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/5/8.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "ResetPasswordViewController.h"

@interface ResetPasswordViewController ()
@property (nonatomic,strong) UITextField *oldPwdTF;
@property (nonatomic,strong) UITextField *PwdTF1;
@property (nonatomic,strong) UITextField *PwdTF2;
@property (nonatomic,strong) UIButton *btn;
@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self resetNav];
    [self setUI];
    
}
- (void)resetNav{
    [self setTheBackBtn];
    self.myTitle.text = @"修改密码";
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(64);
        make.left.mas_offset(0);
        make.height.mas_offset(0);
        make.right.mas_offset(0);
    }];
}
- (void)setUI{
    for (int i=0; i<3; i++) {
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = [PublicMethod colorWithHexString:@"#c8c8c8" andAlpha:1];
        [self.view addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15*WidthRate);
            make.right.mas_offset(-15*WidthRate);
            make.height.mas_offset(0.5);
            make.top.mas_offset(64+60*HeightRate+i*50*HeightRate);
        }];
    }
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.backgroundColor = MAIN_Color;
    [self.btn setTitle:@"修改密码" forState:UIControlStateNormal];
    [self.btn setTitleColor:Bai_Color forState:UIControlStateNormal];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:18*HeightRate];
    [self.btn addTarget:self action:@selector(goToReset) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15*WidthRate);
        make.right.mas_offset(-15*WidthRate);
        make.height.mas_offset(42*HeightRate);
        make.top.mas_offset(200*HeightRate+64);
    }];
    
    self.oldPwdTF = [[UITextField alloc]init];
    self.oldPwdTF.borderStyle = UITextBorderStyleNone;
    self.oldPwdTF.placeholder = @"请输入原密码";
    self.oldPwdTF.secureTextEntry = YES;
    self.oldPwdTF.textColor = Gray_Color;
    self.oldPwdTF.font = [UIFont systemFontOfSize:15*HeightRate];
    [self.oldPwdTF setValue:TiShi_Color forKeyPath:@"_placeholderLabel.textColor"];
    [self.oldPwdTF setValue:[UIFont systemFontOfSize: 15*HeightRate] forKeyPath:@"_placeholderLabel.font"];
    self.oldPwdTF.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.oldPwdTF];
    [self.oldPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(25*WidthRate);
        make.right.mas_offset(-25*WidthRate);
        make.height.mas_offset(50*HeightRate);
        make.top.mas_offset(10*HeightRate+64);
    }];
    
    self.PwdTF1 = [[UITextField alloc]init];
    self.PwdTF1.borderStyle = UITextBorderStyleNone;
    self.PwdTF1.placeholder = @"请输入新密码";
    self.PwdTF1.secureTextEntry = YES;
    self.PwdTF1.textColor = Gray_Color;
    self.PwdTF1.font = [UIFont systemFontOfSize:15*HeightRate];
    [self.PwdTF1 setValue:TiShi_Color forKeyPath:@"_placeholderLabel.textColor"];
    [self.PwdTF1 setValue:[UIFont systemFontOfSize: 15*HeightRate] forKeyPath:@"_placeholderLabel.font"];
    self.PwdTF1.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.PwdTF1];
    [self.PwdTF1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(25*WidthRate);
        make.right.mas_offset(-25*WidthRate);
        make.height.mas_offset(50*HeightRate);
        make.top.mas_offset(60*HeightRate+64);
    }];

    self.PwdTF2 = [[UITextField alloc]init];
    self.PwdTF2.borderStyle = UITextBorderStyleNone;
    self.PwdTF2.placeholder = @"请再次输入新密码";
    self.PwdTF2.secureTextEntry = YES;
    self.PwdTF2.textColor = Gray_Color;
    self.PwdTF2.font = [UIFont systemFontOfSize:15*HeightRate];
    [self.PwdTF2 setValue:TiShi_Color forKeyPath:@"_placeholderLabel.textColor"];
    [self.PwdTF2 setValue:[UIFont systemFontOfSize: 15*HeightRate] forKeyPath:@"_placeholderLabel.font"];
    self.PwdTF2.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.PwdTF2];
    [self.PwdTF2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(25*WidthRate);
        make.right.mas_offset(-25*WidthRate);
        make.height.mas_offset(50*HeightRate);
        make.top.mas_offset(110*HeightRate+64);
    }];

    
}
- (void)goToReset{
    if([self.oldPwdTF.text length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"请输入原密码" animated:YES];

    }else if([self.PwdTF1.text length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"请输入新密码" animated:YES];

    }else if([self.PwdTF2.text length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"请再次输入新密码" animated:YES];

    }else if ([self.PwdTF1.text isEqualToString:self.PwdTF2.text]) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [NetWorkTool resetPasswordWithOldUserPwd:self.oldPwdTF.text userPwd:self.PwdTF1.text block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
            if (data) {
                if ([data[@"success"] integerValue]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"修改成功" animated:YES];
                    [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }else{
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络" animated:YES];
            }
        }];

    }else{
       [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"新密码两次输入不一致" animated:YES];
    }
   }
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
